﻿// created by sanal on 22-23 Jan 2011
try
{
    if (document.getElementById('ctl00_hfMenuSearch').value=="1")
    {
        var mb_menuAnchors = new Array();
        mb_menuAnchors  = mb_menu_getAnchorNodes(document.getElementById('ctl00_tbl_fb_hd'));
        if (mb_menuAnchors.length>0)
        {
            mb_menuDoBubbleSort(mb_menuAnchors,mb_menuAnchors.length);
            mb_addMenuSearchControl();
        }
    }
}
catch(Exc_mnu_srch){}
function mb_menu_getAnchorNodes(fromParentNode)
{
    var tempArr = new Array();
    try
    {
        var anchorNodes = fromParentNode.getElementsByTagName("a");
        for (var i = 0; i < anchorNodes.length; i++) {
            if (anchorNodes[i].getAttribute('href')==null)
                continue;
            if (anchorNodes[i].innerHTML.indexOf('<')<0 && anchorNodes[i].getAttribute('href').indexOf('#')<0)
            {
                tempArr.push(new Array(anchorNodes[i].innerHTML, anchorNodes[i].getAttribute('href')));
            }
        }
    }
    catch(Exc_mnu_srch){}
    return tempArr;
}
function mb_addMenuSearchControl()
{
    try
    {
        var myDiv = document.createElement("div");
        myDiv.id = "mb_mnusrch_div";
        myDiv.className = "mnusrch_div";
//        myDiv.style.width="100px"; // Changes made by bala for reduce the size of navigate to.
//        myDiv.style.height="22px";
//        myDiv.style.align="left";
//        myDiv.style.backgroundColor = '#FFFFFF';
//        myDiv.style.verticalAlign = 'bottom';
//        myDiv.style.marginTop="7px";
        //        myDiv.style.marginLeft="2px";
        //myDiv.innerHTML = "<input tabindex='0' autocomplete='off' class='controlFont' id='mb_txt_mnuSrch' onBlur='mb_mnusrch_onblur()' type='text' style='width:125px;position:absolute;z-index:150;Color:#808080;padding-left:3px;' value='Navigate to..' onFocus='mb_mnusrch_onfocus()' onkeyup='javascript:return mb_interceptMenuSearch(event);' onkeydown='return mb_interceptKeydown(event);' onchange='mb_mnusrch_onchange()' /><div id='mb_div_mnuSrch' style='position:absolute;border:solid 1px #EAEAEA;margin-top:20px;display:none;padding:5px;z-index:99999;background-color:#FFFFFF;'><span class='Text1'>-- Provide Menu Name --</span></div><input type='hidden' id='mb_hdn_mnuSrch' value='-1' />";
        // Changes made by bala for reduce the size of navigate to.
        myDiv.innerHTML = "<div style='border:solid 0px #FF0000;height:20px;'><img id='mb_img_mnuSrch' class='mb_img_mnuSrch' src='images/menusearch.gif' alt='' /><input tabindex='0' autocomplete='off' class='controlFont' id='mb_txt_mnuSrch' onBlur='mb_mnusrch_onblur()' type='text' style='width:74px;position:relative;Color:#808080;padding-left:3px;vertical-align:middle;border:solid 1px #7F9DB9;height:20px;padding-top:2px;border-left:none;' value='Navigate to..' onFocus='mb_mnusrch_onfocus()' onkeyup='javascript:return mb_interceptMenuSearchOnly(event);' onkeydown='return mb_interceptKeydown(event);' /></div><div class='ui-corner-all' id='mb_div_mnuSrch' style='position:absolute;border:solid 1px #EAEAEA;display:none;padding:5px;z-index:99999;background-color:#FFFFFF;margin-top:1px;'><div class='Text1' style='width:135px;text-align:center;'>-- Provide Page Name --</div></div><input type='hidden' id='mb_hdn_mnuSrch' value='-1' />";
        document.getElementById('mb_mnu_srh2').appendChild(myDiv);
    }
    catch(Exc_mnu_srch){}
}
function insertAfter(parent, node, referenceNode) {
  parent.insertBefore(node, referenceNode.nextSibling);
}
function mb_changetxtStyle()
{
    document.getElementById('mb_txt_mnuSrch').style.color = document.getElementById('mb_txt_mnuSrch').value=='Navigate to..' ? '#808080' : '#000000';
}
function mb_mnusrch_onchange()
{
    try
    {
        mb_changetxtStyle();
        document.getElementById('mb_hdn_mnuSrch').value=-1;
        document.getElementById('mb_div_mnuSrch').innerHTML = "<div class='Text1' style='width:135px;text-align:center;padding-bottom:5px;'>-- Provide Page Name --</div>";    
        var vall = document.getElementById('mb_txt_mnuSrch').value.toLowerCase();        
        if (vall=="") return;
        var temp='';
        var j=-1;
        for(var i=0;i<mb_menuAnchors.length;i++)
        {
            if (mb_menuAnchors[i][0].toLowerCase().indexOf(vall)>-1)
            {
                j++;
                //temp = temp + "<div style='width:150px;height:20px;'><a style='vertical-align:middle;padding-left:2px;' class='Text1' onFocus='mb_mnusrchdiv_onfocus(" + j + ");' onMouseOver='mb_menu_changeColor(this);' onMouseOut='mb_menu_changeColorOut(this);' href='" + mb_menuAnchors[i][1] + "'>" + mb_menuAnchors[i][0] + "</a></div>" + "";
                temp = temp.concat("<div class='ui-corner-all' onClick='mb_handleMenuSearchClick(this)' style='line-height:19px;vertical-align:middle;width:135px;height:17px;border:solid 1px #FFFFFF;border-bottom:dotted 1px #EAEAEA;cursor:pointer;padding-top:2px;padding-bottom:2px;'onMouseOver='mb_menu_changeColor(this);' onMouseOut='mb_menu_changeColorOut(this);' ><a style='vertical-align:middle;padding-left:4px;' class='Text1' onFocus='mb_mnusrchdiv_onfocus(" + j + ");' href='" + mb_menuAnchors[i][1] + "'>" + mb_menuAnchors[i][0] + "</a></div>" + "");
            }
        }
        document.getElementById('mb_div_mnuSrch').innerHTML = temp=='' ? "<div class='Text1' style='width:135px;text-align:center;'>No match found.</div>" : temp;
        document.getElementById('mb_div_mnuSrch').style.paddingBottom = temp=='' ? "5px" : "5px";
    }
    catch(Exc_mnu_srch){}
}
function mb_handleMenuSearchClick(th)
{
    try
    {
        //document.location.href = th.childNodes[0].getAttribute('href');
        mb_menuNavigateWithReferrer(th.childNodes[0].getAttribute('href'));
    }
    catch(Exc_mnu_srch){}
}

function mb_menu_changeColor(th)
{
    try
    {
        mb_clearmenuFocus();
        //document.getElementById('mb_div_mnuSrch').focus();
        //th.focus();
        //document.title = th.type;
        //th.style.backgroundColor='#EAEAEA';        
        //document.getElementById('mb_div_mnuSrch').focus();
        th.style.backgroundImage = "url(images/mn_search_bg.jpg)"; 
        th.style.border = "solid 1px #CECECE";
    }
    catch(Exc_mnu_srch){}    
}
function mb_menu_changeColorOut(th)
{
    try
    {
        //th.style.backgroundColor='#FF0000';
        document.getElementById('mb_hdn_mnuSrch').value=-1;
        mb_clearmenuFocus();
    }
    catch(Exc_mnu_srch){}
}
function mb_mnusrch_onfocus()
{
    try
    {
        document.getElementById('mb_div_mnuSrch').style.display = "block";   
        if (document.getElementById('mb_txt_mnuSrch').value=="Navigate to..")
        {
              document.getElementById('mb_txt_mnuSrch').value="";
              mb_changetxtStyle();
        }
        document.getElementById('mb_img_mnuSrch').src = "Images/menusearch_en.gif";
        mb_processEventTrackingEvent();
    }
    catch(Exc_mnu_srch){}
}
//enable google event tracking
function mb_processEventTrackingEvent()
{
    try
    {
        pageTracker._trackEvent('Page Search', 'Focused', 'Page Searched Focused');
    }
    catch(Exc_event_track){}
}
function mb_mnusrch_onblur()
{
    try
    {
        if (document.getElementById('mb_txt_mnuSrch').value=="")
        {
              document.getElementById('mb_txt_mnuSrch').value="Navigate to..";
              mb_changetxtStyle();
        }
        setTimeout(mb_mnusrch_onblur_part2,1500);
    }
    catch(Exc_mnu_srch){}    
}
function mb_mnusrch_onblur_part2()
{
    try
    {
        //if (parseInt(document.getElementById('mb_hdn_mnuSrch').value) == -1)
        {
            document.getElementById('mb_img_mnuSrch').src = "Images/menusearch.gif";        
            document.getElementById('mb_div_mnuSrch').style.display = 'none';
            document.getElementById('mb_hdn_mnuSrch').value = "-1";
        }
    }
    catch(Exc_mnu_srch){}
}
function mb_mnusrchdiv_onfocus(expArg)
{
    try
    {
        document.getElementById('mb_hdn_mnuSrch').value = expArg;
    }
    catch(Exc_mnu_srch){}
}
function mb_clearmenuFocus()
{
    try
    {
        var mbMenuNodes = document.getElementById('mb_div_mnuSrch').childNodes;
        for (var i = 0; i < mbMenuNodes.length; i++) {
            //mbMenuNodes[i].style.backgroundColor = '#FFFFFF';
            mbMenuNodes[i].style.backgroundImage = "none";
            mbMenuNodes[i].style.border = "solid 1px #FFFFFF";
        }
    }
    catch(Exc_mnu_srch){}
}
function mb_setSelected()
{
    try
    {
        curSe = parseInt(document.getElementById('mb_hdn_mnuSrch').value);
        document.getElementById('mb_txt_mnuSrch').value = document.getElementById('mb_div_mnuSrch').childNodes[curSe].childNodes[0].innerHTML;
    }
    catch(Exc_mnu_srch){}
}
function mb_LoadSelectedMenu()
{
    try
    {
        var vall = document.getElementById('mb_txt_mnuSrch').value.toLowerCase();
        if (vall=="") return false;
        var temp='';
        var j=-1;
        for(var i=0;i<mb_menuAnchors.length;i++)
        {
            if (mb_menuAnchors[i][0].toLowerCase()==vall)
            {
                temp = mb_menuAnchors[i][1];
                break;
            }
        }
        if (temp.length>0)
        {
            //document.location.href = temp;  
            document.getElementById('mb_txt_mnuSrch').value = "Loading...";  
            document.getElementById('mb_div_mnuSrch').style.display = 'none'; 
            mb_menuNavigateWithReferrer(temp);
        }
    }
    catch(Exc_mnu_srch){}
    return true;
}
function mb_menuNavigateWithReferrer(url)//solves the referrer not set issue in IE
{
    try
    {
        var fakeLink = document.createElement ("a");
        if (typeof(fakeLink.click) == 'undefined' || typeof(fakeLink.click) == 'function')
        {
            document.location.href = url;  // sends referrer in FF, not in IE
        }
        else
        {
            fakeLink.href = url;
            document.body.appendChild(fakeLink);
            fakeLink.click();   // click() method defined in IE only
        }
    }
    catch(Exc){}
}
function mb_interceptKeydown(e)
{
    try
    {
        mb_interceptMenuSearch(e);//03/04/12 - sks (flipped the events for the control between up and down)
        var code;
        if (!e) var e = window.event;
        if (e.keyCode) code = e.keyCode;
        else if (e.which) code = e.which;
        var intKey = code;
        if (intKey == 13) { //enter key
            //document.getElementById('mb_div_mnuSrch').style.display = 'none';
            mb_LoadSelectedMenu();
            return false;
        }
        return true;
    }
    catch(Exc_mnu_srch){}
}
function mb_interceptMenuSearch(e)
{

    try
    {
//        e.cancelBubble = true;
//        if (e.stopPropagation) e.stopPropagation();  
        var curSel = -1;
        var code;
        if (!e) var e = window.event;
        if (e.keyCode) code = e.keyCode;
        else if (e.which) code = e.which;
        var KKey = code;
        if (KKey == 38)//up
        {
            curSel = parseInt(document.getElementById('mb_hdn_mnuSrch').value);
            if (curSel>0)
            {
                if (document.getElementById('mb_div_mnuSrch').childNodes.length>1)
                {
                    mb_clearmenuFocus();
                    curSel--;
                    document.getElementById('mb_hdn_mnuSrch').value = curSel;
                    //document.getElementById('mb_div_mnuSrch').childNodes[curSel].style.backgroundColor = '#EAEAEA';
                    document.getElementById('mb_div_mnuSrch').childNodes[curSel].style.backgroundImage = "url(images/mn_search_bg.jpg)"; 
                    document.getElementById('mb_div_mnuSrch').childNodes[curSel].style.border = "solid 1px #CECECE";
                    mb_setSelected();
                }
            }     
            return false;       
        }
        else if (KKey == 40)//down
        {
            //if (curSel>0)
            {
                if (document.getElementById('mb_div_mnuSrch').childNodes.length>0)
                {
                    if (document.getElementById('mb_div_mnuSrch').childNodes[0].innerHTML != "No match found." && document.getElementById('mb_div_mnuSrch').childNodes[0].innerHTML != "-- Provide Page Name --")
                    {
                        curSel = parseInt(document.getElementById('mb_hdn_mnuSrch').value);
                        if (curSel<(document.getElementById('mb_div_mnuSrch').childNodes.length-1))
                        {
                            mb_clearmenuFocus();
                            curSel++;
                            document.getElementById('mb_hdn_mnuSrch').value = curSel;
                            //document.getElementById('mb_div_mnuSrch').childNodes[curSel].style.backgroundColor = '#EAEAEA';
                            document.getElementById('mb_div_mnuSrch').childNodes[curSel].style.backgroundImage = "url(images/mn_search_bg.jpg)"; 
                            document.getElementById('mb_div_mnuSrch').childNodes[curSel].style.border = "solid 1px #CECECE";
                            mb_setSelected();
                        }
                    }
                }
            }
            return false; 
        }
        else
        {   
            //mb_mnusrch_onchange();//03/04/12 - sks
            return true;
        }
    }
    catch(Exintercept){}
    return true;
}
function mb_interceptMenuSearchOnly(e)//03/04/12 - sks
{

    try
    {
//        e.cancelBubble = true;
//        if (e.stopPropagation) e.stopPropagation();  
        var curSel = -1;
        var code;
        if (!e) var e = window.event;
        if (e.keyCode) code = e.keyCode;
        else if (e.which) code = e.which;
        var KKey = code;
        if (KKey == 38)//up
        {
            return false;       
        }
        else if (KKey == 40)//down
        {
            return false; 
        }
        else
        {   
            mb_mnusrch_onchange();
            return true;
        }
    }
    catch(Exintercept){}
    return true;
}
function mb_menuDoBubbleSort(arrayName, length) 
{
    for (var i = 0; i < (length-1); i++)
    {
        for (var j = i+1; j < length; j++)            
        {
            if (arrayName[j] < arrayName[i]) 
            {
                var dummy = arrayName[i];
                arrayName[i] = arrayName[j];
                arrayName[j] = dummy;
            }
        }
    }  
}
